﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cognex.VisionPro;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.FGGigE;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro.Implementation;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.FGGigE.Implementation.Internal;
using Cognex.VisionPro.PMAlign;
using System.Windows.Forms;
using System.Drawing;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.ToolGroup;
using System.Runtime.InteropServices;
using System.Management;
using Cognex.VisionPro.OCRMax;
using Cognex.VisionPro.PixelMap;
using System.IO;

namespace Delta
{
    public enum Vpptype {myCogToolBlock1, myCogToolBlock2}

    public delegate void DelegateLoad(Vpptype vpptype, string message);
    
    public class Cog
    {
        public static event DelegateLoad VppLoad;
        public static CogToolBlock myCogToolBlock;
        public static CogToolBlock myCogToolBlock1, myCogToolBlock2;
        Form1 form1;

        public Cog(Form1 form)
        {
            form1 = form;
            VppLoad += new DelegateLoad(Cog_VppLoad);
        }

        public static void SaveVpp(Vpptype vpptype, string path)
        {
            if (vpptype == Vpptype.myCogToolBlock1)
            {
                CogSerializer.SaveObjectToFile(myCogToolBlock1, path , typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter), CogSerializationOptionsConstants.Minimum);
            }
            
        }

        public static void LoadVpp(string path, string path2, bool isFirstInitial, Vpptype vpptype)
        {
            if (isFirstInitial == false)
            {
                if (vpptype == Vpptype.myCogToolBlock1)
                {

                    try
                    {
                        myCogToolBlock1 = CogSerializer.LoadObjectFromFile(path + @"\DeltaVpp.vpp", typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter), CogSerializationOptionsConstants.Minimum) as CogToolBlock;
                        VppLoad(Vpptype.myCogToolBlock1, "");
                    }
                    catch (Exception)
                    {
                        try
                        {
                            myCogToolBlock1 = CogSerializer.LoadObjectFromFile(path2 + @"\DeltaVpp.vpp", typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter), CogSerializationOptionsConstants.Minimum) as CogToolBlock;
                        }
                        catch (Exception)
                        {
                            //mainform.logger.Info("产品1视觉程序加载失败，请确认视觉程序路径和文件是否存在");
                            VppLoad(Vpptype.myCogToolBlock1, "视觉程序加载失败，请确认视觉程序路径和文件是否存在");
                            Form1.logger.Error("视觉程序加载失败，请确认视觉程序路径和文件是否存在");
                        }
                    }
                }
            }

        }

        private void Cog_VppLoad(Vpptype vpptype, string message)
        {
            if (vpptype == Vpptype.myCogToolBlock1)
            {
                if (string.IsNullOrEmpty(message))
                {
                 form1.formVpToolBlock. cogToolBlockEditV21.Subject = myCogToolBlock1;

                }
            }


        }

       

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="image">需要保存的原图</param>
        /// <param name="Station">图片所在工位：</param>
        public void OneSaveImage(CogImage8Grey image, int Station)
        {
            try
            {
                switch (Station)
                {
                    case 1:
                        string strOKFileName_Product1 = @"D:\Image\OKImage\" + form1.layoutInformation.CurrentMaterial + @"\" + DateTime.Now.ToString("yyyy-MM-dd");
                        string strNGFileName_Product1 = @"D:\Image\NGImage\" + form1.layoutInformation.CurrentMaterial + @"\" + DateTime.Now.ToString("yyyy-MM-dd");
                        if (form1.IsSaveOKImg == true)
                        {
                            if (!Directory.Exists(strOKFileName_Product1))
                            {
                                Directory.CreateDirectory(strOKFileName_Product1);
                            }
                            if (form1.formVpToolBlock.Cam1Result[0] == 1)
                            {
                                form1.formVpToolBlock.cogImageFile.InputImage = image;
                                form1.formVpToolBlock.cogImageFile.Operator.Open(strOKFileName_Product1 + @"\" + DateTime.Now.ToString("HHMMssff") + ".jpg", CogImageFileModeConstants.Write);
                                form1.formVpToolBlock.cogImageFile.Run();
                            }
                        }

                        if (form1.IsSaveNGImg == true)
                        {
                            if (!Directory.Exists(strNGFileName_Product1))
                            {
                                Directory.CreateDirectory(strNGFileName_Product1);
                            }
                            if (form1.formVpToolBlock.Cam1Result[0] != 1)
                            {
                                form1.formVpToolBlock.cogImageFile.InputImage = image;
                                form1.formVpToolBlock.cogImageFile.Operator.Open(strNGFileName_Product1 + @"\" + DateTime.Now.ToString("HHMMssff") + ".jpg", CogImageFileModeConstants.Write);
                                form1.formVpToolBlock.cogImageFile.Run();
                            }
                        }
                        break;
                }

                GC.Collect();
            }
            catch (Exception ex)
            {
                Form1.logger.Error(ex.ToString());
                MessageBox.Show(ex.ToString());
            }
        }







    }

}
